Python3 字符串格式化 您所在的位置:网站首页 python3 字符串 find Python3 字符串格式化

Python3 字符串格式化

2024-06-16 18:50| 来源: 网络整理| 查看: 265

字符串的格式化方法分为两种,分别为占位符(%)和format方式。占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越广,format方式使用的更加广泛。

一 占位符(%)

 

%d 实例(Python3.0+): age = 29 print("my age is %d" %age) #my age is 29 %s 实例(Python3.0+): name = "makes" print("my name is %s" %name) #my name is makes %f 实例(Python3.0+): print("%6.3f" % 2.3) #2.300 print("%f" %2.3) #2.300000 二 format方法 位置映射

实例(Python3.0+): print("{}:{}".format('192.168.0.100',8888)) #192.168.0.100:8888 关键字映射

实例(Python3.0+): print("{server}{1}:{0}".format(8888,'192.168.1.100',server='Web Server Info :')) #Web Server Info :192.168.1.100:8888   元素访问

实例(Python3.0+): print("{0[0]}.{0[1]}".format(('baidu','com'))) #baidu.com  填充对齐 ^、分别是居中、左对齐、右对齐

实例1(Python3.0+): print("{0}*{1}={2:0>2}".format(3,2,2*3)) #3*2=06 print("{:*^30}".format('centered')) #***********centered***********  实例2(Python3.0+):九九乘法表 for i in range(1,10): a = 1 while a


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有